package com.ruoyi.fetv.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.aspect.annotation.AutoDict;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.fetv.controller.vo.Label;
import com.ruoyi.fetv.controller.vo.school.AddReq;
import com.ruoyi.fetv.controller.vo.school.EditReq;
import com.ruoyi.fetv.domain.FetvInformation;
import com.ruoyi.fetv.domain.FetvSchool;
import com.ruoyi.fetv.service.IFetvSchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 高校Controller
 *
 * @author Jack
 * @date 2023-03-07
 */
@Api(value = "/fetv/school", tags = {"高校管理"})
@RestController
@RequestMapping("/fetv/school")
@Validated
@AutoDict
public class FetvSchoolController extends BaseController {
    @Autowired
    private IFetvSchoolService fetvSchoolService;


    /**
     * 根据id获得name
     */
    @ApiImplicitParam(paramType = "path", dataType = "Long", name = "id", value = "", required = true)

    @ApiOperation(value = "根据ID查询栏目的名字", notes = "根据ID查询栏目的名字", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('fetv:context:list')")
    @GetMapping("/name/{id}")
    @ApiResponse(code = 200, message = "", response = String.class)
    public String getNameById(@PathVariable(value = "id") String id) {
        return fetvSchoolService.getNameById(id);
    }

    /**
     * 获根据学校名字获得id和name
     */
    @ApiImplicitParam(paramType = "path", dataType = "String", name = "query", value = "", required = false)
    @ApiOperation(value = "查询学校列表的标签", notes = "查询学校列表的标签", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('fetv:context:list')")
    @GetMapping("/label")
    @ApiResponse(code = 200, message = "", response = List.class)
    public List<Label> list(@RequestParam(value = "query", required = false) String name) {
        // 方便空参请求
        if (name == null) {
            name = "";
        }
        return fetvSchoolService.getIdAndNameByName(name);
    }

    /**
     * 查询高校列表
     */

    @ApiImplicitParam(paramType = "query", dataTypeClass = FetvSchool.class, name = "fetvSchool", value = "", required = true)
    @ApiOperation(value = "查询高校列表", notes = "查询高校列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('fetv:school:list')")
    @ApiResponse(code = 200, message = "", response = TableDataInfo.class)
    @GetMapping("/list")
    public TableDataInfo<List<FetvInformation>> list(FetvSchool fetvSchool) {
        startPage();
        List<FetvSchool> list = fetvSchoolService.selectFetvSchoolList(fetvSchool);
        return getDataTable(list);
    }

    /**
     * 获取高校详细信息
     */

    @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "", required = true)
    @ApiOperation(value = "获取高校详细信息", notes = "根据ID获取高校详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('fetv:school:query')")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功操作的数量", response = FetvSchool.class),
            @ApiResponse(code = 500, message = "不存在的ID")
    })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        FetvSchool data = fetvSchoolService.selectFetvSchoolById(id);
        if (data == null) {
            throw new ServiceException("不存在的ID");
        }
        return success(data);
    }

    /**
     * 新增高校信息
     */
    @ApiOperation(value = "新增高校", notes = "新增高校", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('fetv:school:add')")
    @Log(title = "高校", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiResponse(code = 200, message = "成功操作的数量", response = int.class)
    public AjaxResult add(@ApiParam(name = "fetvSchool", value = "高校信息", required = true)
                          @RequestBody @Valid AddReq fetvSchool) {
        return toAjax(fetvSchoolService.insertFetvSchool(fetvSchool));
    }

    /**
     * 修改高校
     */

    @ApiOperation(value = "修改高校信息", notes = "修改高校信息", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('fetv:school:edit')")
    @Log(title = "高校", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiResponse(code = 200, message = "成功操作的数量", response = int.class)
    public AjaxResult edit(@ApiParam(name = "fetvSchool", value = "高校信息", required = true)
                           @RequestBody @Valid EditReq fetvSchool) {
        return toAjax(fetvSchoolService.updateFetvSchool(fetvSchool));
    }

    /**
     * 删除高校
     */
    @ApiImplicitParam(paramType = "path", dataType = "Long[]", name = "ids", value = "", required = true)
    @ApiOperation(value = "删除高校", notes = "根据ID删除高校", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('fetv:school:remove')")
    @ApiResponse(code = 200, message = "成功操作的数量", response = int.class)
    @Log(title = "高校", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fetvSchoolService.deleteFetvSchoolByIds(ids));
    }
}
